package cn.slipi.admin.common.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;

/**
 * 日期时间工具类
 *
 * @author lee
 * @date 2019-03-17
 * @since 1.0.0
 */
public class DateUtil {


    private static final Integer PRE_MS = 1000;
    private static final String FORMAT_DATE = "yyyy.MM.dd";
    private static final String FORMAT_DATE_TIME = "yyyy.MM.dd HH:mm:ss";
    private static final String FORMAT_TIME = "HH:mm:ss";
    private static final String FORMAT_SORT_TIME = "HH:mm";

    /**
     * 获取时间戳
     *
     * @param date
     * @return
     */
    public static Integer getTimeBySecond(Date date) {
        if (null == date) {
            return 0;
        }
        String timestamp = String.valueOf(date.getTime());
        int length = timestamp.length();
        if (length > 3) {
            return Integer.valueOf(timestamp.substring(0, length - 3));
        } else {
            return 0;
        }
    }

    public static Long getTimeByMinSecond() {
        return System.currentTimeMillis();
    }

    public static Long dayStartStamp() {
        // 获取当前日期
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        return calendar.getTimeInMillis();
    }

    public static Long dayEndStamp() {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.HOUR_OF_DAY, 23);
        calendar.set(Calendar.MINUTE, 59);
        calendar.set(Calendar.SECOND, 59);
        calendar.set(Calendar.MILLISECOND, 999);
        return calendar.getTimeInMillis();
    }

    public static Long preDayStartStamp(Integer x) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(calendar.get(Calendar.YEAR),
                calendar.get(Calendar.MONTH),
                calendar.get(Calendar.DAY_OF_MONTH) - x,
                0, 0, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        return calendar.getTime().getTime();
    }

    public static Long preDayEndStamp(Integer x) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(calendar.get(Calendar.YEAR),
                calendar.get(Calendar.MONTH),
                calendar.get(Calendar.DAY_OF_MONTH) - x,
                23, 59, 59);
        calendar.set(Calendar.MILLISECOND, 999);
        return calendar.getTime().getTime();
    }

    public static Long dateToTimeStamp(String dateStr, String format) {
        try {
            SimpleDateFormat sdf = new SimpleDateFormat(format);
            return sdf.parse(dateStr).getTime() / PRE_MS;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0L;
    }

    public static String getGWT(int yearAmount) {
        Calendar cd = Calendar.getInstance();
        //向后推迟时间
        cd.add(Calendar.YEAR, yearAmount);
        SimpleDateFormat sdf = new SimpleDateFormat("EEE d MMM yyyy HH:mm:ss 'GMT'", Locale.US);
        // 设置时区为GMT
        sdf.setTimeZone(TimeZone.getTimeZone("GMT"));
        return sdf.format(cd.getTime());
    }

    public static String getFormatDate(Date date) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(FORMAT_DATE_TIME);
        return simpleDateFormat.format(date);
    }

    /**
     * 时间戳转日期
     *
     * @param stamp
     * @return
     */
    public static String stampToDate(Long stamp, String format) {
        format = StrUtils.isBlank(format) ? FORMAT_DATE : format;
        String res;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
        Date date = new Date(stamp);
        res = simpleDateFormat.format(date);
        return res;
    }

    public static Date getPreMonth(Integer num) {
        Calendar c = Calendar.getInstance();
        c.setTime(new Date());
        c.add(Calendar.MONTH, -num);
        return c.getTime();
    }

    public static String formatMilliSecond(long ms) {
        // 一秒内
        if (ms <= PRE_MS) {
            return ms + "ms";
        }
        // 一秒到一分钟之内
        if (ms % PRE_MS == 0) {
            return String.format("%ss", ms / PRE_MS);
        }
        double ds = (double) ms / PRE_MS;
        return String.format("%.3fs", ds);
    }

    public static Long strToTimestamp(String date, String format) {
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        try {
            return sdf.parse(date).getTime();
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return 0L;
    }

    public static String dateFormatToString(Date date, String format) {
        SimpleDateFormat dateFormat = new SimpleDateFormat(format);
        return dateFormat.format(date);
    }


}
